package com.fml.easy;

/**
 * 罗马数字转整数
 */
public class Demo04 {

    public int romanToInt(String s) {
        int i = 1;
        int v = 5;
        int x = 10;
        int l = 50;
        int c = 100;
        int d = 500;
        int m = 1000;
        //特殊情况
        int iv = v - i;
        int ix = x - i;
        int xl = l - x;
        int xc = c - x;
        int cd = d - c;
        int cm = m - c;

        String temp = s;
        int[] arr = {iv, ix, xl, xc, cd, cm};
        int[] specnum = {};
        String[] num = {};
        String[] arrStr = {"IV", "IX", "XL", "XC", "CD", "CM"};
        int nums = 0;
        for (int j = 0; j < arrStr.length; j++) {
            if (s.contains(arrStr[j])) {
                num[j] = s.substring(0, 1 + s.indexOf(arrStr[j]));
                specnum[nums] = arr[j];
                nums++;
                s = s.substring(2 + s.indexOf(arrStr[j]));
            } else {
                s = s.substring(1 + s.indexOf(arrStr[j]));
            }

        }


        return -1;
    }


    public static void main(String[] args) {
        System.out.println( romanToInt1("IIII"));;
    }
    public static int romanToInt1(String s) {

        int num = 0;
        String[] split = s.split("");
        for (int j = 0; j < split.length-1 ; j++) {
            String temp =  temp = split[j] + split[j + 1];


            if (convert(temp) == 0) {

                num += convert(split[j]);
                if (j == split.length - 2) {
                    num += convert(split[j + 1]);
                }
            } else {
                num += convert(temp);
                j++;
                if (j == split.length - 2) {
                    j--;
                }
            }



        }


        return num;
    }

    private  static  int convert(String s) {
        int i = 1;
        int v = 5;
        int x = 10;
        int l = 50;
        int c = 100;
        int d = 500;
        int m = 1000;
        //特殊情况
        int iv = v - i;
        int ix = x - i;
        int xl = l - x;
        int xc = c - x;
        int cd = d - c;
        int cm = m - c;
        int[] arrnum = {i, v, x, l, c, d, m, iv, ix, xl, xc, cd, cm};

        String[] arrStr = {"I", "V", "X", "L", "C", "D", "M", "IV", "IX", "XL", "XC", "CD", "CM"};
        for (int j = 0; j < arrStr.length; j++) {
            if (arrStr[j].equals(s)) {
                return arrnum[j];
            }
        }
        return 0;
    }
}


